package com.romotc.qqrobot.webqq.qqInterfaceCatching;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.romotc.common.utils.RandomString;
import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.scache.RequestSpy;
import com.romotc.scache.WebConnection;
/**
 * 登录成功之后的catching 返回给webQqUi合适的数据 
 * 保证虚拟号能登录成功
 * 保证在线滴答的用户能重新正常登录进来
 * @author Administrator
 *
 */
public class Login2_Catching extends QQUrlBodyCatching {

	@Override
	public boolean modifyResponse(WebConnection wc, RequestSpy rqSpy)
			throws IOException {
		return false;
		
	}

	@Override
	/**
	 * {"retcode":0,"result":{"uin":1586030808,"cip":1971856781,
	 * 		"index":1075,"port":45315,"status":"online",
	 * 		"vfwebqq":"393b9dc49195cd035caa9c260313fc7b7bcf838ad6b1e92ba79c48494600edad7415994813be577a",
	 * 		"psessionid":"8368046764001d636f6e6e7365727665725f77656271714031302e3133342e362e31333800003dbe0000143c036e0400d8e8885e6d0000000a40616e6f7a7436685a776d00000028393b9dc49195cd035caa9c260313fc7b7bcf838ad6b1e92ba79c48494600edad7415994813be577a",
	 * 		"user_state":0,"f":0}}

		sshproxy@qq.com:
		{"retcode":0,"result":{"uin":1586030808,"cip":1971856781,
				"index":1075,"port":45320,"status":"online",
				"vfwebqq":"71b58bfb568cec5e2346edff4b2a703171e8de0c178e9b3b0c24d9e948f39c94f28ba6e6492b5255",
				"psessionid":"8368046764001d636f6e6e7365727665725f77656271714031302e3133342e362e31333800004ea40000143c036e0400d8e8885e6d0000000a40616e6f7a7436685a776d0000002871b58bfb568cec5e2346edff4b2a703171e8de0c178e9b3b0c24d9e948f39c94f28ba6e6492b5255",
				"user_state":0,"f":0}}

	 */
	String getHackReqBodyStr(RequestSpy requestSpy,Map<String, Object> var) {
		String loginEmail = requestSpy.getReqCookiesJson().optString(WebQQConstant.Cookie_ptui_loginuin);
		if(WebQQConstant.isRobotUin(loginEmail)) {
			
			QQLogedin qqLogedin = new QQLogedin();
			String uin = requestSpy.getReqCookiesJson().optString(WebQQConstant.COOKIE_uin);
			if(StringUtils.isNotBlank(uin) && uin.startsWith("o")){
				uin = uin.substring(1);//去掉前面的o
				qqLogedin.setUin(uin);
			}
			
			qqLogedin.setCip(1971856781);
//			qqLogedin.set
			qqLogedin.setStatus("online");
			qqLogedin.setVfwebqq(RandomString.randomString(80));
			qqLogedin.setPsessionid(RandomString.randomString(225));
			qqLogedin.setUser_state(0);
			qqLogedin.setF(0);
			
			return qqLogedin.toString();
		}else {
			//在线滴答用户
			QQLogedin curQQ = QQLogedin.getCurQqLogedIn(requestSpy);
			if(curQQ != null && curQQ.active) {
				//活跃的 不许要生成直接可以使用
				curQQ.stopKeepOnlineThread();//提前调 防止当前登录的 和滴答线程的登录冲突
				return curQQ.toString();
			}
		}
		
		return null;
	}

	@Override
	/**
	 * Cache-Control:no-cache
		Connection:Keep-Alive
		Content-Length:458
		Content-Type:text/plain; charset=utf-8
		Date:Wed, 25 Jan 2012 03:35:07 GMT
		Proxy-Connection:Keep-Alive
	 */
	ArrayList<String> hackRequestHeader(RequestSpy requestSpy, int bodySize ,Map<String, Object> var) {
		ArrayList<String> headers = new ArrayList<String>();
		headers.add("Cache-Control:no-cache");
		headers.add("Connection:Keep-Alive");
		headers.add("Content-Length:" + bodySize);
		headers.add("Date:Wed, 25 Jan 2012 03:35:07 GMT");
		headers.add("Content-Type:text/plain; charset=utf-8");
		headers.add("Proxy-Connection:Keep-Alive");
		headers.add("dataFrom: Login2_Catching");
		return headers;
	}

	@Override
	boolean isGoOn(RequestSpy requestSpy) {
		// TODO Auto-generated method stub
		return false;
	}

}
